package com.viewpagertext.fragments;

import android.media.MediaPlayer;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.viewpagertext.R;
import com.viewpagertext.adapters.LocalFoldExpandableListviewAdapter;
import com.viewpagertext.constructor.Song;
import com.viewpagertext.utils.LoadLocalMusicUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class LocalAlbumFragment extends Fragment {

    ExpandableListView expandableListView;
    MediaPlayer mediaPlayer;

    private List<String> albums;
    private HashMap<String, List<Song>> songs;

    LocalFoldExpandableListviewAdapter localFoldExpandableListviewAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_local_fold, container, false);
        expandableListView = view.findViewById(R.id.local_fold_expandable_listview);
        initView();
        return view;
    }

    private void initView() {
        albums = new ArrayList<>();
        songs = new HashMap<>();
        mediaPlayer = new MediaPlayer();

        List<Song> allSongs = LoadLocalMusicUtils.getMusic();
        for (Song song : allSongs) {
            String album = song.album;
            if (!albums.contains(album)) {
                albums.add(album);
            }
            List<Song> songList = songs.get(album);
            if (songList == null) {
                songList = new ArrayList<>();
            }
            songList.add(song);
            songs.put(album, songList);
        }
        localFoldExpandableListviewAdapter = new LocalFoldExpandableListviewAdapter(requireContext(), albums, songs, "album");
        expandableListView.setAdapter(localFoldExpandableListviewAdapter);

    }

}